package com.apes.autoparts.supplier.jikugroup;

import com.apes.autoparts.supplier.platform.SupplierPlatformItem;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lishikun on 2021/3/15 11:34
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = JiKuGroupDebug.CONST_TABLE_NAME)
public class JiKuGroupDebug extends Model {
    public static final String CONST_TABLE_NAME = "ji_ku_group_debug";
    @Id
    @Comment("流水码")
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "积准")
    private String id;

    @Comment("供应商")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "supplier_platform_item_id", referencedColumnName = "id")
    private SupplierPlatformItem supplierPlatformItem;

    @Comment("配件唯一Id")
    @Column(name = "parts_id", nullable = false)
    private Integer partsId;

    @Comment("配件唯一自编码")
    @Column(name = "parts_code")
    private String partsCode;

    @Comment("配件名称")
    @Column(name = "parts_name")
    private String partsName;

    @Comment("原厂编码")
    @Column(name = "origin_parts_code")
    private String originPartsCode;

    @Comment("配件类别")
    @Column(name = "parts_category_name")
    private String partsCategoryName;

    @Comment("配件品牌")
    @Column(name = "parts_brand_name")
    private String partsBrandName;

    @Comment("原产地")
    @Column(name = "origin_place")
    private String originPlace;

    @Comment("单位")
    @Column(name = "unit_name")
    private String unitName;

    @Comment("备注")
    @Column(name = "remark")
    private String remark;

    @Comment("规格")
    @Column(name = "spec")
    private String spec;

    @Comment("车品牌")
    @Column(name = "car_brand_name")
    private String carBrandName;

    @OneToMany(mappedBy = "jiKuGroupDebug", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<JiKuGroupDebugItem> stockDTOs = new ArrayList<>();

    public List<JiKuGroupDebugItem> getStockDTOs() {
        if (stockDTOs == null) return new ArrayList<>();
        for (JiKuGroupDebugItem item : stockDTOs) {
            // 设置主表
            item.setJiKuGroupDebug(this);
        }
        return stockDTOs;
    }
}
